package com.shuosen.gmall.cart.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shuosen.gmall.bean.CartInfo;
import com.shuosen.gmall.config.LoginRequire;
import com.shuosen.gmall.service.CartService;
import com.shuosen.gmall.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class CartController {


    @Reference
    private CartService  cartService  ;

    @Autowired
    private CartCookieHandler   cartCookieHandler  ;

    @Reference
    private ManageService manageService  ;

    @RequestMapping("addToCart")
    @LoginRequire(autoRedirect = false )
    public String addToCart(HttpServletRequest request, HttpServletResponse response){
        //获取到页面提交过来的数据
        String skuNum = request.getParameter("skuNum");
        String skuId = request.getParameter("skuId");


        //如何获取userId
        String  userId = (String) request.getAttribute("userId");
        //判断登录
        if(userId!=null){
            //调用服务层
            cartService.addToCart(skuId,userId,Integer.parseInt(skuNum));  //登录
        }else{
            //未登录添加
             // 因为 request.setAttribute("userId",userId);
            cartCookieHandler.addToCart(request,response,skuId,userId,Integer.parseInt(skuNum));
        }
        //保存skuInfo
        request.setAttribute("skuInfo",manageService.getSkuInfo(skuId));

        //保存购买的数量
        request.setAttribute("skuNum",skuNum);
        return  "success";
    }


    @RequestMapping("cartList")
    @LoginRequire(autoRedirect = false)
    public String cartList(HttpServletRequest request,HttpServletResponse response){
        //判断用户是否登录 ，登录了从redis中获取 redis中没有 从数据库获取  
        //没有登录 从cookie中取得  
        String userId = (String) request.getAttribute("userId");
             List<CartInfo> cartList = null ;
        if(userId!=null){
            //从cookie中查找购物车
             List<CartInfo> cartListFromCookie =  cartCookieHandler.getCartList(request);
             if(cartListFromCookie!=null && cartListFromCookie.size()>0){
                  //开始合并
                 cartList = cartService.mergeToCartList(cartListFromCookie,userId);
                 //删除cookie中的购物车
                 cartCookieHandler.deleteCartCooke(request,response);

             }else{
                 //从redis中取得 或者从数据库中
                 cartList  =  cartService.getCartList(userId);
             }
        }else{
            //未登录 从cookie中获取
             cartList =  cartCookieHandler.getCartList(request);
        }
        //保护作用域
        request.setAttribute("cartInfoList",cartList);
        return "cartList" ;
    }

    @RequestMapping("checkCart")
    @ResponseBody
    @LoginRequire(autoRedirect =  false )
    public void checkCart(HttpServletRequest  request , HttpServletResponse response){
          String skuId =  request.getParameter("skuId");
          String isChecked =  request.getParameter("isChecked");
          String userId =  request.getParameter("userId");
          if(userId!=null){
               cartService.checkCart(skuId,isChecked,userId);
          }else{
              cartCookieHandler.checkCart(request,response,skuId,isChecked);
          }
    }

    @RequestMapping("toTrade")
    @LoginRequire(autoRedirect = true)
    public String trade(HttpServletRequest request,HttpServletResponse response ){
        String  userId = (String) request.getAttribute("userId");
        List<CartInfo> cookieHandlerCartList = cartCookieHandler.getCartList(request);
        if(cookieHandlerCartList!=null && cookieHandlerCartList.size()>0){
            cartService.mergeToCartList(cookieHandlerCartList,userId);
            cartCookieHandler.deleteCartCooke(request,response);
        }
        return "redirect://order.gmall.com/trade" ;
    }





}
